

use processed/cpisave/cpi_master.dta,clear

keep I	datayear weight	quant b* 

bysort datayear: egen double Itot = total(I*weight)
bysort datayear: egen double wtot = total(weight)
forvalues x = 1/85	 {
gen e`x' = b`x'*I*weight
bysort datayear: egen double e`x'tot = total(e`x')
} 
drop b* I

forvalues x = 1/85	 {
gen b`x' = e`x'tot/Itot
}
 
drop Itot	wtot e* weight quant

duplicates drop datayear,force

merge m:1 datayear using processed/cpisave/dlogcpi_master.dta
drop _merge 

reshape long b dlogp, i(datayear) j(goods_id)
	xtset goods_id  datayear
	gen  dlogTOR_tmp = (1/2)*(l.b+b)*dlogp 
	bysort datayear : egen dlogTOR = total(dlogTOR_tmp)
	duplicates drop datayear,force

	gen dlog_TOR_CUM = 0 if datayear == 2001
		tset datayear
		forvalues tloop = 2002/2017 {
	replace dlog_TOR_CUM = l.dlog_TOR_CUM + dlogTOR if `tloop' == datayear
        } 	

rename dlog_TOR_CUM log_TOR
keep log_TOR
outsheet using DataforMatlab\torn_CPI.csv, comma replace nonames

	
	
